package com.team.vlog_temp.Controller;

import com.team.vlog_temp.dto.LoginRequest;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin // 允许所有来源的跨域请求
public class LoginController {

    @PostMapping("/getUser") // 定义POST请求的路径
    public Map<String, Object> login(@RequestBody LoginRequest loginRequest, HttpSession session) {
        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();
        String userCaptcha = loginRequest.getCaptcha(); // 获取用户输入的验证码

        System.out.println(username);
        System.out.println(password);
        System.out.println(userCaptcha);

        Map<String, Object> map = new HashMap<>();

        // 从会话中获取存储的验证码
        String sessionCaptcha = (String) session.getAttribute("captcha");

        // 验证用户名、密码和验证码
        if ("zhaoyan".equals(username) && "123".equals(password) && sessionCaptcha != null && sessionCaptcha.equalsIgnoreCase(userCaptcha)) {
            map.put("data", 1); // 登录成功
            return map;
        } else {
            map.put("data", 0); // 登录失败
            return map;
        }
    }
}
